<?php


namespace app\controller\app\mine;


use App\controller\app\AppBase;
use app\model\UserModel;
use app\service\Route;
use support\Request;
use think\facade\Cache;
use Webman\Http\Response;

class Mine extends AppBase
{
    public function beforeAction(){
        $rtn = parent::beforeAction();
        if($rtn instanceof Response){
            return $rtn;
        }
    }

    /**
     * description:个人信息
     * @Route(reqUrl="/app.mine.info")
     * author:yangkelong
     * data:2022/8/25
     */
    public function info(){
        $userObj = new UserModel([],$this->app_id);
        $info = $userObj->info($this->user_id);
        if($info instanceof Response){
            return $info;
        }
        else{
            Cache::set('auth_'.$this->user_id,$info,60*60*24*365);
            $info['access_token']=$this->access_token;
            return success($info);
        }
    }

    /**
     * description:修改密码
     * @Route(reqUrl="/app.mine.edtPwd")
     * author:yangkelong
     * data:2022/8/25
     */
    public function edtPwd(){
        $userObj = new UserModel([],$this->app_id);
        $userInfo = $userObj::find($this->user_id);
        if($userInfo['password']!=md5(input('oldPassword'))){
            return error(1,"旧密码错误");
        }
        $where[]=['user_id','=',$this->user_id];
        $res = $userObj->where($where)->update(['password'=>md5(input('password')),'edt_time'=>time()]);
        return $res ? success(null,0,"密码修改成功") : error(1,"密码修改失败");
    }

    /**
     * description:修改个人信息
     * @Route(reqUrl="/app.mine.edtInfo")
     * author:yangkelong
     * data:2022/8/25
     */
    public function edtInfo(){
        $userObj = new UserModel([],$this->app_id);
        $res = $userObj->edt(['edt_time'=>time()]);
        return ($res instanceof Response) ? $res : ($res ? success($res,0,"修改成功") : error(1,"修改成功"));
    }

    /**
     * description:修改密码
     * @Route(reqUrl="/app.mine.edt.pwd.bycaptcha")
     * author:yangkelong
     * data:2022/8/23
     */
    public function edtPwdByCaptcha(){
        $code=input('code');
        $mobile=input('mobile');
        if($code==Cache::get($mobile.'_captcha')){
            $userObj = new UserModel([],$this->app_id);
            $where[]=['user_id','=',$this->user_id];
            $res = $userObj->where($where)->update(['password'=>md5(input('password')),'edt_time'=>time()]);
            return $res ? success(null,0,"密码修改成功") : error(1,"密码修改失败");
        }
        else{
            return error(1,"验证码错误");
        }

    }
}